var promise = require('bluebird');
var logger = require('winston');
var fs = promise.promisifyAll(require('fs-extra'));
var utils = require('./FooUtils');
var math = require('mathjs');
promise.promisifyAll(require('./FooUtils.js'));

var global_test_dir = '/Users/Steve/tmp/nodeJs/';
var global_test_file = global_test_dir + 'test.txt';
var isFileExist = false;

utils.cleanDirAsync('/Users/Steve/tools/myGit/tenmalife-platform/src/main/webapp/js/MemberSearchComponent.min.js').then(function(){
	console.log('ok.');
}).catch(function(err){
	console.log('error:%',err);
});

// executeSystemCommand();
//openSystemBrowser();
return false;
// mhoMasterParseJson();
// executeCommonTest();
// executeFilesTest();

function openSystemBrowser() {
    utils.openSystemDefaultBrowser("https://www.zhihu.com/question/21357452");
}

function executeSystemCommand() {
    const msg = "curl 'http://gamer.qq.com/search/mhocsvdata?key=%E6%99%93%E9%A3%8E&iType=5&iPageNum=0&iPageSize=50&iMatchAll=0&iColumn=1%7C2&tk=8c31351ba0e7b2a16852fbad67d95f0b&_=1460364768792' -H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Referer: http://gamer.qq.com/p/resource/weaponlist/iProductID/39' -H 'X-Requested-With: XMLHttpRequest' -H 'Cookie: o_cookie=1248771031; _ga=GA1.2.57034428.1456300119; eas_sid=h1h435i9K9Y9f9q2f3z1h252k6; ts_refer=www.baidu.com/link; ptcz=8e10760095910129db052c8e3e29e072e22343f83de6a904a2b451baeb447692; pt2gguin=o1248771031; qm_username=272079879; qm_sid=5ee4d1092f6c96a39856bd6163e8963e,cU8evs-Kb0vE.; ptisp=ctc; ptui_loginuin=272079879; PHPSESSID=1ksn6lpvmjtl9bi9entkc6c106; _pk_ref.1.7614=%5B%22%22%2C%22%22%2C1460364709%2C%22http%3A%2F%2Fgamer.qq.com%2Fp%2Fresource%2Farmorlist%2FiProductID%2F39%22%5D; pgv_pvi=255361024; pgv_si=s2164467712; pgv_info=ssid=s3625138450; ts_last=gamer.qq.com/p/resource/weaponlist/iProductID/39; pgv_pvid=5244886880; ts_uid=886894368; _pk_id.1.7614=2e9f7557ffb54c81.1459996147.5.1460364752.1460364709.; _pk_ses.1.7614=*; iUserAgent=s%3Aj%3A%7B%22value%22%3A%22ba89cec0-ffc2-11e5-a76c-af2283e281db%22%7D.seGzKl9FnoBiBc9WU0KcT12EfqSqpLOuQjbjbFQUHkI' -H 'Connection: keep-alive' -H 'Cache-Control: no-cache' --compressed";
    const exec = require('child_process').exec;
    const child = exec(msg,
        (error, stdout, stderr) => {
            console.log(utils.parseJson(stdout));
// console.log(`stdout: ${stdout}`);
// console.log(`stderr: ${stderr}`);
            if (error !== null) {
                console.log(`exec error: ${error}`);
            }
        });
}

function mhoMasterParseJson() {
    utils.readJsonAsync("/Users/Steve/tmp/test.json").then(function (res) {
        res.result.rows.forEach(function (item, index, array) {
            console.log("name:%s,defence:%s", item.data[1], item.data[6]);
        });
    });
}


function executeFooUtilsTest() {
    var obj = {'sex': 'male', "year": 12};
    var json = utils.toJson(obj);
    logger.info(json);
    logger.info(utils.parseJson(json));

    utils.readJsonAsync("/Users/Steve/tmp/test.json").then(function (res) {
        console.log(res.year);
    });
    return false;

    var dir = "/Users/Steve/tmp/test.json";
    utils.generateUUID().then(function (res) {
        logger.info(res);
    }).catch(function (err) {
        logger.error(err);
    });
    utils.formatFloatAsync(123.334).then(function (res) {
        logger.info(res);
    }).catch(function (err) {
        logger.error(err);
    });
}
function executeFilesTest() {
    /**
     * File test,Start
     */
    var filePromise = fs.accessAsync(global_test_file).then(function () {
        logger.info('delete test file at initial.');
        fs.unlink(global_test_file);
        return "deleteOne";
    }).catch(function (err) {
        logger.error('test file of:【%s】do not exist,now ready to create one for test.', global_test_file);
        return "createOne";
    });

    // note: filePromise point to a new location now.
    filePromise = filePromise.then(function () {
        logger.info('write new file now.');
        fs.writeFile(global_test_file, 'Hello node.js.');
    }).then(function () {
        logger.info('append content to test file.');
        fs.appendFile(global_test_file, ' This is appended content.');
    }).then(function () {
        logger.info('reading test file.');
        return fs.readFileAsync(global_test_file);
    }).then(function (res) {
        logger.info('read file over,content is:%s', res);
    }).catch(function (err) {
        logger.error('error occours as:%s', err);
    });

    filePromise.then(function () {
        return fs.statAsync(global_test_file);
    }).then(function (stat) {
        if (stat.isFile()) {
            logger.info('delete file at end:%s', stat);
            fs.unlink(global_test_file);
        }
    }).catch(function (err) {
        logger.error('error occours as:%s', err);
    });

    /**
     * File test,End
     */
}